package com.yb.bussiness.goods;

import java.util.List;

import com.yb.base.dao.BaseDao;
import com.yb.base.dao.IBaseDao;
import com.yb.domain.goods.TgProduct;
import com.yb.util.framework.PageUtil;

public class ProductServiceImpl implements ProductService {
	private IBaseDao<TgProduct, Long> dao;

	public ProductServiceImpl() {
		dao = new BaseDao<TgProduct, Long>(TgProduct.class);
	}

	public PageUtil findByBrandId(Long brandId, int page, int limit, String name) {
		String sql = "select *";
		String countSql = "select count(*)";
		String sqlTemp = " from t_g_product t where t.brandId = ? ";
		if (!"".equals(name)) {
			sqlTemp += "and name like '%" + name + "%'";
		}
		sqlTemp += "order by createTime desc";
		List<TgProduct> list = dao.findList(sql + sqlTemp + " limit ?,?",
				new Object[] { brandId, (page - 1) * limit, limit });
		List<Object[]> countList = dao.findObjectArryList(countSql + sqlTemp,
				new Object[] { brandId });
		int count = 0;
		Object[] objAry = countList.get(0);
		count = new Integer(objAry[0].toString());
		PageUtil pu = new PageUtil();
		pu.setList(list);
		pu.setPageNo(page);
		pu.setPageSize(limit);
		pu.setTotalRecords(count);
		return pu;
	}

	public TgProduct read(Long id) {
		return dao.read(id);
	}

	public void deleteByIds(String ids) {
		String[] idAry = ids.split("_");
		for (int i = 0; i < idAry.length; i++) {
			Long id = new Long(idAry[i]);
			dao.deleteById(id);
		}
	}

	public void save(TgProduct product) {
		dao.saveOrUpdate(product);
	}
}
